import wx

class ConsoleWindow(wx.TextCtrl):
    def __init__(self, parent, id, size):
        wx.TextCtrl.__init__(self, parent, id, "", size=size, style=wx.TE_MULTILINE|wx.TE_RICH2)
        self.SetOwnBackgroundColour("black")
        self.SetDefaultStyle(wx.TextAttr("white","black"))
        self.SetEditable(False)
        
        self.addLine("Whoopy de poopy", "me", (255,255,0))
        self.addLine("Whoopy de poopy", "me", (0,255,255))
        self.addLine("Whoopy de poopy", "me")
        self.addLine("LLLLEEEEEEEEERRRRRRRRROOOOOOOOOOOOOOYYYYYYYYYY  JJJJJJEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEENNNNNNNNNNNNNNNNNNKKKKKKKKKKKKKKKKIIIIIIIIIIIIIIIIIIINNNNNNNNNNNNNNNNNSSSSSSSSSSS", "Leroy", color="red")
       
    def addLine(self, line, source="", color="white"):
        numchars = 0
        defstyle = self.GetDefaultStyle()
        self.SetDefaultStyle(wx.TextAttr(color, "black"))
        if source != "":
            self.AppendText(source+"> ")
            numchars += len(source) + 2
        self.AppendText(line+"\n")
        self.SetDefaultStyle(defstyle)
                

class ConsoleControl(wx.Panel):
    def __init__(self, parent, id, size):
        wx.Panel.__init__(self, parent, id, size)
        (w,h) = size
        if h < 20: h = 20
        self.inputLine = wx.TextCtrl(self, id, "", size=(w,20), style=wx.TE_PROCESS_ENTER) #style=wx.TE_PROCESS_ENTER
##        self.SetEditable(True)
        self.window = ConsoleWindow(self, wx.ID_ANY, size=(w,h-20))
        self.Bind(wx.EVT_TEXT_ENTER, self.OnEnter)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.window, 1, wx.EXPAND)
        sizer.Add(self.inputLine, 0, wx.EXPAND)
        self.SetSizer(sizer)
        
    def OnEnter(self, event):
        line = self.inputLine.GetValue()
        if len(line): self.window.addLine(line, "me")
        self.inputLine.SetValue("")


if __name__ == '__main__':
    def OnKey(event):
        chat.inputLine.SetFocusFromKbd()
        chat.inputLine.EmulateKeyPress(event)        
        
    app = wx.PySimpleApp()
    frame = wx.Frame(None, wx.ID_ANY, "Chat stuff testapp..")
    chat = ConsoleControl(frame, wx.ID_ANY, (600,200))
    frame.Bind(wx.EVT_CHAR, OnKey)
    frame.Fit()
    frame.Show()
    app.MainLoop()